package wiiusej.apptest.generalTasks;

import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import wiiusej.Wiimote;


public class ActivateSpeaker implements Callable{

	static Logger fileLogger;	
	private final Wiimote myWiimote;
	private AtomicBoolean speakerStatus;
	
	private final SpeakerConfigurations speakerConfig = 
		new SpeakerConfigurations();
	
	
	public ActivateSpeaker(Wiimote wiimote, AtomicBoolean speakerStatus2){
	
		this.myWiimote = wiimote;
		this.speakerStatus = speakerStatus2;
		
		PropertyConfigurator.configure("log4j.properties");
		this.fileLogger = Logger.getLogger("speaker");
	}
	
	
	
	@Override
	public AtomicBoolean call() throws Exception {
				
		if(!speakerStatus.get()){
    		
			//myWiimote.activateSpeaker();
			adpcmConfig();
			speakerStatus.set(true);
    		fileLogger.info("Speaker Activated");   	
    		//myWiimote.sendSound();    		
    		
    	}else{
    		speakerStatus.set(false);
    		myWiimote.deactivateSpeaker();    		
    		fileLogger.info("Speaker Deactivated");
    	}
		
		
		return speakerStatus;
	}
	
	
	private void pcmConfig(){
		
		fileLogger.debug("Config: " + speakerConfig.getPcmConfig()
				+ "SampleRate: " + speakerConfig.getPcmdefaultconfig());
		
		myWiimote.activateSpeaker(speakerConfig.getPcmConfig(), 
				speakerConfig.getPcmconfig3k());		
	}
	
	private void adpcmConfig(){
		myWiimote.activateSpeaker(speakerConfig.getApcmConfig(), 
				speakerConfig.getApcmconfig12k());
	}

}
